<template>
  <el-dialog
    :title="title"
    :visible="dialogVisible"
    width="50%"
    @close="dialogClose"
  >
    <el-form
      :model="editFrom"
      :rules="addFromRules"
      ref="addEditFormRef"
      label-width="70px"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="editFrom.username"
          :disabled="!!editFrom.id"
        ></el-input>
      </el-form-item>

      <el-form-item
        type="password"
        label="密码"
        prop="password"
        v-if="!editFrom.id"
      >
        <el-input v-model="editFrom.password"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="editFrom.email"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="editFrom.mobile"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogClose">取 消</el-button>
      <el-button type="primary" @click="confirmFrom">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addUser, editGetUser } from '@/api/users'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '添加用户'
    },
    editFrom: {
      type: Object,
      default() {
        return {
          username: '',
          password: '',
          email: '',
          mobile: ''
        }
      }
    }
  },
  data() {
    const checkEmail = (rule, value, cb) => {
      const regEmail = /^\w+@\w+(\.\w+)+$/
      if (regEmail.test(value)) {
        return cb()
      }
      cb(new Error('请输入合法的邮箱'))
    }
    const checkMobile = (rule, value, cb) => {
      const regMobile = /^1[34578]\d{9}$/
      if (regMobile.test(value)) {
        return cb()
      }
      cb(new Error('请输入合法的手机号码'))
    }
    return {
      addFromRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '用户名长度在 3 到 10 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 12,
            message: '密码长度在 6 到 12 个字符',
            trigger: 'blur'
          }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {
            validator: checkEmail,
            message: '邮箱格式不正确，请重新输入',
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          {
            validator: checkMobile,
            message: '手机号码不正确，请重新输入',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    dialogClose() {
      this.$emit('update:dialogVisible', false)
      this.$refs.addEditFormRef.resetFields()
    },
    // 确认提交
    confirmFrom() {
      if (this.title === '添加用户') {
        this.addUser()
      }
      if (this.title === '修改用户信息') {
        this.editUserBtn()
      }
    },
    // 修改
    editUserBtn() {
      this.$refs.addEditFormRef.validate(async valid => {
        if (!valid) return
        const res = await editGetUser(this.editFrom)
        if (res.meta.status === 200) {
          this.$message.success('修改用户成功')
          this.$parent.loadUserList()
        } else {
          this.$message.error('修改用户失败')
        }
        this.dialogClose()
      })
    },
    // 新增
    async addUser() {
      this.$refs.addEditFormRef.validate(async valid => {
        if (!valid) return
        const res = await addUser(this.editFrom)
        if (res.meta.status === 201) {
          this.$message.success('添加用户成功')
          this.$parent.loadUserList()
        } else {
          this.$message.error('添加用户失败')
        }
        this.dialogClose()
      })
    }
  }
}
</script>
